<?php
require_once('includes/head.php');

require_once("languages/pl/head1.php");

$arrLevel = array(100, 200, 300, 400, 500, 600, 700, 800, 900, 100);
$arrPow = array(50, 250, 500, 1000, 2500, 5000, 10000, 25000, 50000, 100000);
$intLevels = 0;
foreach ($arrLevel as $intLevel)
{
    if ($player -> level < $intLevel)
    {
        $expn = (pow($player -> level,2) * $arrPow[$intLevels]);
        break;
    }
    $intLevels ++;
}

$pct = (($player -> exp / $expn) * 100);
$pct = round($pct,"0");


$query = $db -> Execute("SELECT count(*) FROM `log` WHERE `unread`='F' AND `owner`=".$player -> id);
$numlog = $query -> fields['count(*)'];
$query -> Close();

/**
 * Graph bars
 */
if ($player -> graphic != '' || $player -> graphbar == 'Y') 
{
    $intExpperc = $pct;
    if ($pct > 97)
    {
        $intExpperc = 97;
    }
    $intVial = 97 - $intExpperc;
    $intPerhealth = (($player -> hp / $player -> max_hp) * 100);
    $intPerhealth = round($intPerhealth, '0');
    $strHealth = $intPerhealth;
    if ($intPerhealth > 97)
    {
        $intPerhealth = 97;
    }
    $intVial2 = 97 - $intPerhealth;
    $cape = $db -> Execute("SELECT `power` FROM `equipment` WHERE `owner`=".$player -> id." AND `type`='C' AND `status`='E'");
    $maxmana = (($player -> inteli + $player -> wisdom + $player -> cond) * 0.7);
    $maxmana = $maxmana + (($cape -> fields['power'] / 100) * $maxmana);
    $cape -> Close();
    $intPermana = (($player -> mana / $maxmana) * 100);
    $intPermana = round($intPermana, '0');
    $strMana = $intPermana;
    if ($intPermana > 97)
    {
        $intPermana = 97;
    }
    $intVial3 = 97 - $intPermana;
    $smarty -> assign(array("Barsize" => $intPerhealth,
                            "Healthper" => $strHealth,
                            "Barsize2" => $intPermana,
                            "Manaper" => $strMana,
                            "Expper" => $intExpperc,
                            "Vial" => $intVial,
                            "Vial2" => $intVial2,
                            "Vial3" => $intVial3));
}

$malyres = date("i");
$ilemaly = 59-$malyres; 

$querypriv = $db -> Execute("SELECT count(*) FROM priv WHERE unread='F' AND ownerid=".$player -> id);
$numpriv = $querypriv -> fields['count(*)'];
$querypriv -> Close();

if ($numpriv > 0) $numpriv = "<blink>".$numpriv."</blink>";
/*
if ($player -> location == 'Altara' || $player -> location == 'Ardulith' || $player -> location == 'Port')
{
	$lokac = $player -> location;
} else $lokac = 'Altara';
*/
$intCtime = time() - 420;

$nazwaspotk = $db -> Execute("SELECT nazwa FROM spotk_pok WHERE pokoj=".$player -> pokoj);
$nazwaspotkpokoj = $nazwaspotk -> fields['nazwa'];
$nazwaspotk -> Close();

$numoc = 0;
$numClima = 0;
$numSpotka1 = 0;
$numSpotka2 = 0;
$numPrivv = 0;
$wszchaty = $db -> Execute("SELECT page FROM players WHERE (page='Chat' OR page='Rozmowa' OR page='Karczma' OR page='Spotkanie z ".$player -> pokoj."' OR page like 'Spotkanie z%') AND lpv>=".$intCtime);
while (!$wszchaty -> EOF)
{
	switch ($wszchaty -> fields['page']) {
	case 'Chat': $numClima = $numClima + 1; break;
	case 'Rozmowa': $numPrivv = $numPrivv + 1; break;
	case 'Karczma': $numoc = $numoc + 1; break;
	case 'Spotkanie z '.$player -> pokoj: $numSpotka1 = $numSpotka1 + 1; break;
	default: $numSpotka2 = $numSpotka2 + 1; break;
	}
	$wszchaty -> MoveNext();
}
$numSpotka2 = $numSpotka2 + $numSpotka1;
$wszchaty -> Close();

if ($player -> location == 'Altara' || $player -> location == 'Ardulith' || $player -> location == 'Port')
{
$inn = '<li><a href="karczma.php">Karczma ['.$numoc.']</a></li>';
} else $inn = '';

$query = $db -> Execute("SELECT count(*) FROM players WHERE refs=".$player -> id." AND cube > 0 AND level > 10 AND age > 7");
$ref = $query -> fields['count(*)'];
$query -> Close();

//losowanie headera
if (ereg("^layout*", $player -> style))
{
$stylll = str_replace("layout", "", $player -> style);
$stylll = str_replace(".css", "", $stylll);
    $path = 'images/'.$stylll.'/';
    $dir = opendir($path);
    $arrnamea = array();
    $r = 0;
    while ($file = readdir($dir)) 
    {
        if (ereg("_.png*$", $file) && ereg("gotowy", $file)) 
        {
            $arrnamea[$r] = $file;
            $r = $r + 1;
        }
    }
    closedir($dir);
$smarty -> assign (array ("obrazekved" => $path.$arrnamea[rand(0, $r-1)],
                                "stylll" => $stylll));
}
$time = date("H:i:s");
$smarty -> assign (array ("Time" => $time,
                          "Date" => $newdate,
                          "Inn" => $inn,
                          "Malyres" => $ilemaly,
                          "Priv" => $numpriv,
                          "Rangaa" => $player -> rank,
                          "Title" => $title,
                          "Clima" => $numClima,
                          "Spotka1" => $numSpotka1,
                          "Privv" => $numPrivv,
                          "Spotka2" => $numSpotka2,
                          "Nazwapokoj" => $nazwaspotkpokoj,
                          "Pokojid" => $player -> pokoj,
                          "Name" =>  $player -> tag." ".$player -> user." ".$player -> nazwisko,
                          "Id" => $player -> id,
                          "Level" => $player -> level,
                          "Exp" => $player -> exp,
                          "Expneed" => $expn,
                          "Percent" => $pct,
                          "Health" => $player -> hp,
                          "Maxhealth" => $player -> max_hp,
                          "Mana" => $player -> mana,
                          "Energy" => $player -> energy,
                          "Maxenergy" => $player -> max_energy,
                          "Gold" => $player -> credits,
                          "Bank" => $player -> bank,
                          "Mithril" => $player -> platinum,
                          "Numlog" => $numlog,
                          "Style" => $player -> style,
                          "Graphbar" => $player -> graphbar,
                          "Hospital" => '',
                          "Battle" => '',
                          "Tribe" => '',
                          "Lbank" => '',
                          "Special" => '',
                          "Tforum" => '',
                          "Spells" => '',
                          "Location" => '',
                          "Referals" => $ref,
                          "Plevel" => LEVEL,
                          "Exppts" => EXP_PTS,
                          "Healthpts" => HEALTH_PTS,
                          "Manapts" => MANA_PTS,
                          "Energypts" => ENERGY_PTS,
                          "Goldinhand" => GOLD_IN_HAND,
                          "Goldinbank" => GOLD_IN_BANK,
                          "Hmithril" => MITHRIL,
                          "Navigation" => NAVIGATION,
                          "Gametime" => GAME_TIME,
                          "Nstatistics" => N_STATISTICS,
                          "Nitems" => N_ITEMS,
                          "Nequipment" => N_EQUIPMENT,
                          "Nlog" => N_LOG,
                          "Nnotes" => N_NOTES,
                          "Npost" => N_POST,
                          "Nforums" => N_FORUMS,
                          "Ninn" => N_INN,
                          "Noptions" => N_OPTIONS,
                          "Nlogout" => N_LOGOUT,
                          "Nhelp" => N_HELP,
                          "Nmap" => N_MAP,
                          "Stephead" => ''));

$arrFilename = explode("/", $_SERVER['PHP_SELF']);
$intKey = count($arrFilename) - 1;
$strFilename = $arrFilename[$intKey];
if (isset($_GET['step']) && $strFilename == 'newspaper.php')
{
    $arrFile = array('');
    $smarty -> assign(array("Stephead" => $_GET['step'],
                            "Linksfile" => $arrFile));
}
    else
{
    $objLinks = $db -> Execute("SELECT `id`, `file`, `text` FROM `links` WHERE `owner`=".$player -> id." ORDER BY `id` ASC");
    $arrFile = array('');
    $arrText = array('');
    $intLinks = 0;
    if ($objLinks -> fields['id'])
    {
        while (!$objLinks -> EOF)
        {
            $arrFile[$intLinks] = $objLinks -> fields['file'];
            $arrText[$intLinks] = $objLinks -> fields['text'];
            $intLinks ++;
            $objLinks -> MoveNext();
        }
        $objLinks -> Close();
    }
    $smarty -> assign(array("Linksfile" => $arrFile,
                            "Linkstext" => $arrText,
                            "Linksnum" => $intLinks));
}

if ($player -> clas != 'Barbarzyńca') 
{
    $smarty -> assign ("Spells", "<li><a href=\"czary.php\">".SPELLS_BOOK."</a></li>");
}

$smarty -> assign ("Lbank", "<li><a href=\"bank.php\">Bank</a><br><br></li>");

switch ($player -> location) {
case 'Altara':
case 'Ardulith':
case 'Port':
    $smarty -> assign(array("Location" => "<li><a href=\"city.php\">".CITY."</a></li>",
                            "Battle" => "<li><a href=\"battle.php\">".B_ARENA."</a></li>"));
    if ($player -> tribe) 
    {
        $smarty -> assign ("Tribe", "<li><a href=\"tribes.php?view=my\">".MY_TRIBE."</a></li>");
    } break;
case 'Podróż':
    $test = $db -> Execute("SELECT quest FROM questaction WHERE player=".$player -> id." AND action!='end'");
    if ($test -> fields['quest'] != 0) 
    {
        $qlocation = $db -> Execute("SELECT location FROM quests WHERE qid=".$test -> fields['quest']);
        $smarty -> assign("Location", "<li><a href=\"".$qlocation -> fields['location']."?step=quest\">".RETURN_TO."</a></li>");
        $qlocation -> Close();
    }
        else
    {
        if (!isset($_GET['step']))
        {
            $_GET['step'] = 'caravan';
        }
        $smarty -> assign("Location", "<li><a href=\"travel.php?akcja=".$_GET['akcja']."&amp;step=".$_GET['step']."\">".RETURN_TO2."</a></li>");
    }
    $test -> Close(); break;
case 'Góry':
    if ($player -> fight == 0) {
        $smarty -> assign ("Location", "<li><a href=\"gory.php\">".MOUNTAINS."</a></li>");
    } 
        else 
    {
        $smarty -> assign ("Location", "<li><a href=\"explore.php?akcja=gory\">".MOUNTAINS."</a></li>");
    } break;
case 'Las':
    if  ($player -> fight == 0) 
    {
        $smarty -> assign ("Location", "<li><a href=\"las.php\">".FOREST."</a></li>");
    } 
        else 
    {
        $smarty -> assign ("Location", "<li><a href=\"explore.php?akcja=las\">".FOREST."</a></li>");
    } break;
case 'Lochy':
	$smarty -> assign ("Location", "<li><a href=\"jail.php\">".JAIL."</a></li>");
break;
case 'Portal':
    $smarty -> assign ("Location", "<li><a href=\"portal.php\">".PORTAL."</a></li>");
break;
case 'Astralny plan':
    $objFight = $db -> Execute("SELECT `fight` FROM `players` WHERE `id`=".$player -> id);
    $smarty -> assign ("Location", "<li><a href=\"portals.php?step=".$objFight -> fields['fight']."\">".ASTRAL_PLAN."</a></li>");
    $objFight -> Close();
break;
}


$unread = $db -> Execute("SELECT count(*) FROM `mail` WHERE `owner`=".$player -> id." AND `zapis`='N' AND `unread`='F' AND `send`=0");
$intUnreadmails = $unread -> fields['count(*)'];
$unread -> Close();

$intUnreadmails ? $strUnread = "<blink>".$intUnreadmails."</blink>" : $strUnread = $intUnreadmails;
$smarty -> assign ("Unread", $strUnread);

//$player -> tribe ? $smarty -> assign ("Tforum", "<li><a href=\"tforums.php?view=topics\">".T_FORUM."</a></li>");

switch ($player -> rank) {
case 'Admin':
	$zappoliczz = $db -> Execute("SELECT count(*) FROM kandydat WHERE wysw='T' AND (rank='Member' OR rank='Przybysz' OR rank='Obywatel')");
	$policzz = $zappoliczz -> fields['count(*)'];
	$zappoliczz -> Close();
	if ($policzz) 
	{
	$strpolicz = "<blink>".$policzz."</blink>";
	} else $strpolicz = $policzz;
	$smarty -> assign ("Special", "<li><a href=\"admin.php\">".KING." [".$strpolicz."]</a></li>
	<li><a href=\"forums.php?view=categories\">Forum wewnetrzne</a></li>"); break;
	case 'Staff':
	$zappoliczz = $db -> Execute("SELECT count(*) FROM kandydat WHERE wysw='T' AND (rank='Member' OR rank='Przybysz' OR rank='Obywatel')");
	$policzz = $zappoliczz -> fields['count(*)'];
	$zappoliczz -> Close();
	if ($policzz) 
	{
	$strpolicz = "<blink>".$policzz."</blink>";
	} else $strpolicz = $policzz;
	$smarty -> assign ("Special", "<li><a href=\"staff.php\">".PRINCE." [".$strpolicz."]</a></li>
	<li><a href=\"forums.php?view=categories\">Forum wewnetrzne</a></li>"); break;
	case 'Sędzia':
	$smarty -> assign ("Special", "<li><a href=\"sedzia.php\">".JUDGE."</a></li>
	<li><a href=\"forums.php?view=categories\">Forum wewnetrzne</a></li>"); break;
	case 'Radny':
	$smarty -> assign ("Special", "<li><a href=\"forums.php?view=categories\">Forum wewnetrzne</a></li>"); break;
	case 'Karczmarka':
	$smarty -> assign ("Special", "<li><a href=\"forums.php?view=categories\">Forum wewnetrzne</a></li>"); break;
	case 'Redaktor':
	$smarty -> assign ("Special", "<li><a href=\"forums.php?view=categories\">Forum wewnetrzne</a></li>"); break;
	case 'Skryba':
	$smarty -> assign ("Special", "<li><a href=\"forums.php?view=categories\">Forum wewnetrzne</a></li>"); break;
	case 'MG':
	$smarty -> assign ("Special", "<li><a href=\"mg.php\">Mistrz Gry</a></li>
	<li><a href=\"forums.php?view=categories\">Forum wewnetrzne</a></li>"); break;
	case 'Gwardzista':
	$zappoliczz = $db -> Execute("SELECT count(*) FROM kandydat WHERE wysw='T' AND (rank='Member' OR rank='Przybysz' OR rank='Obywatel')");
	$policzz = $zappoliczz -> fields['count(*)'];
	$zappoliczz -> Close();
	if ($policzz) 
	{
	$strpolicz = "<blink>".$policzz."</blink>";
	} else $strpolicz = $policzz;
	$smarty -> assign ("Special", "<li><a href=\"gwardzista.php\">Gwardzista [".$strpolicz."]</a></li>
	<li><a href=\"forums.php?view=categories\">Forum wewnetrzne</a></li>"); break;
	case 'Kapitan Gwardii':
	$zappoliczz = $db -> Execute("SELECT count(*) FROM kandydat WHERE wysw='T' AND (rank='Member' OR rank='Przybysz' OR rank='Obywatel')");
	$policzz = $zappoliczz -> fields['count(*)'];
	$zappoliczz -> Close();
	if ($policzz) 
	{
	$strpolicz = "<blink>".$policzz."</blink>";
	} else $strpolicz = $policzz;
	$smarty -> assign ("Special", "<li><a href=\"gwardzista.php\">Kapitan [".$strpolicz."]</a></li>
	<li><a href=\"forums.php?view=categories\">Forum wewnetrzne</a></li>"); break;
}

// mini poradnik dla nowych graczy
$porada = '';
if (($player -> race == '' || $player -> clas == '' || $player -> race2 == '' || $player -> gender == '') && $player -> rank == 'Przybysz') $porada = '<b>Witaj w '.$gamename.'!</b> Na początku wypełnij informacje o Twojej postaci w <a href="stats.php">Statystykach</a>.';
elseif (($player -> user == 'Nieznajomy' || $player -> avatar == '') && $player -> rank == 'Przybysz') $porada = 'Następną rzeczą którą powinieneś zrobić, jest wybranie <a href="account.php?view=name">imienia</a> oraz <a href="account.php?step=view">avatara</a>.';
elseif ($player -> rank == 'Przybysz') $porada = 'Wszystko ustawione? Aby być pełnoprawnym graczem Santic musisz jeszcze złożyć <a href="kandydat.php">podanie</a>. Nie jest to krok konieczny, możesz grać będąc przybyszem. Miejsca do których na pewno będziesz często zaglądał to: <a href="city.php">Miasto</a>, <a href="battle.php"> Arena walk</a> i <a href="chat.php">karczma</a>. Jeśli masz jakieś pytania, zadaj je na <a href="clima.php">czacie</a> lub gwardzistom (ci z tarczą na liście graczy). Bądź wyrozumiała/y dla Gwardzistów :) - trzymają się <a href="court.php?step=rules">prawa</a>. Energia odnawia się co godzinę.';
$smarty -> assign ("Porada", $porada);

/*
$Zart = $db -> SelectLimit("SELECT * FROM `zart` WHERE czas='0' order by id desc", 8);
$arrZart1 = array();
$arrAutor1 = array();
$arrAutorid1 = array();
$i=0;
while (!$Zart -> EOF) 
{
	$arrZart1[$i] = $Zart -> fields['tresc'];
	$arrAutor1[$i] = $Zart -> fields['autor'];
	$arrAutorid1[$i] = $Zart -> fields['autorid'];
	$Zart -> MoveNext();
	$i = $i + 1;
}
$rzutt = rand(0,4);
$smarty -> assign ("Zart", $arrZart1[$rzutt]." - opowiedział ".$arrAutor1[$rzutt]." (".$arrAutorid1[$rzutt].")");
$Zart -> Close();
*/

$smarty -> display ('header.tpl');
?>
